<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <div th:replace="header::common"></div>
    <script type="text/javascript" th:src="@{/static/echarts.min.js}"></script>
    <style>
        .charts-row {
            display: flex;
            justify-content: space-around;
            margin-bottom: 20px; /* 为每行添加一些间隔 */
        }
        .chart {
            width: 48%; /* 每个图表占行宽的一半 */
            height: 400px;
        }
    </style>
</head>
<body>
<!-- 第一行图表 -->
<div class="charts-row">
    <div id="main1" class="chart"></div>
    <div id="main2" class="chart"></div>
</div>

<!-- 第二行图表 -->
<div class="charts-row">
    <div id="main3" class="chart" style="width: 600px; margin: 0 auto;"></div>
    <div id="main4" class="chart" style="width: 600px; margin: 0 auto;"></div>
</div>
<script type="text/javascript">
    // 基于准备好的dom，初始化echarts实例
    var myChart1 = echarts.init(document.getElementById('main1'));
    var myChart2 = echarts.init(document.getElementById('main2'));
    var myChart3 = echarts.init(document.getElementById('main3'));
    var myChart4 = echarts.init(document.getElementById('main4'));
    $.post(
        '/echarts/selectCarTypeCount',
        function (result) {
            console.log(result);
            let xArray = new Array();
            let yArray = new Array();
            let data = result.data;
            for (let i = 0; i < data.length; i++) {
                xArray.push(data[i].name);
                yArray.push(data[i].value);
            }
            // 指定图表的配置项和数据
            let option1 = {
                title: {
                    text: '汽车所属种类'
                },
                tooltip: {},
                legend: {
                    data: ['数量']
                },
                xAxis: {
                    data: xArray
                },
                yAxis: {},
                series: [
                    {
                        name: '数量',
                        type: 'bar',
                        data: yArray,
                        showBackground: true,
                        backgroundStyle: {
                            color: 'rgba(220, 220, 220, 0.8)'
                        }
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            myChart1.setOption(option1);

            //饼状图
            let option2 = {
                title: {
                    text: '汽车所属种类',
                    subtext: '',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [
                    {
                        name: 'Access From',
                        type: 'pie',
                        radius: '50%',
                        data: result.data,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            myChart2.setOption(option2);

            let option3 = {
                title: {
                    text: '汽车种类',
                    left: 'center',
                    top: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [
                    {
                        name: '所属种类',
                        type: 'pie',
                        radius: ['40%', '70%'],
                        data: result.data
                    }
                ]
            };
            myChart3.setOption(option3);

            let option4 = {
                xAxis: {
                    data: xArray
                },
                yAxis: {},
                series: [
                    {
                        data: yArray,
                        type: 'line',
                        smooth: true
                    }
                ]
            };
            myChart4.setOption(option4);
        },
        'json'
    );
</script>
</body>
</html>